package com.jxd.petMgr.service.impl;

import com.jxd.petMgr.dao.IAccountDao;
import com.jxd.petMgr.dao.IPetDao;
import com.jxd.petMgr.dao.IPetOwnerDao;
import com.jxd.petMgr.dao.impl.*;
import com.jxd.petMgr.model.Account;
import com.jxd.petMgr.model.Pet;
import com.jxd.petMgr.model.PetOwner;
import com.jxd.petMgr.service.IPetOwnerService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lzh
 * @version 1.0
 * @Description
 * @date 2021/11/3
 */


public class PetOwnerServiceImpl implements IPetOwnerService {
    IPetOwnerDao petOwnerDao = new PetOwnerDaoImpl();
    IPetDao petDao = new PetDaoImpl();
    IAccountDao accountDao = new AccountDaoImpl();


    /**
     * 获得所有主人对象
     *
     * @return 主人对象集合
     */
    @Override
    public List<PetOwner> getAllOwenr() {
        return petOwnerDao.getAllOwenr();
    }

    /**
     * 输入用户名和密码返回主人对象
     *
     * @param name     用户名
     * @param password 密码
     * @return 主人对象
     */
    @Override
    public PetOwner login(String name, String password) {
        PetOwner petOwner = null;
        int id = petOwnerDao.getOwnerID(name, password);
        if (id != 0) {
            petOwner = petOwnerDao.getOwnerByID(id);
        }

        return petOwner;
    }

    /**
     * 购买宠物
     * 传入一个宠物对象,更改宠物主人
     * 主人元宝数减少,商店元宝增加，添加新帐目
     *
     * @param pet 宠物对象
     */
    @Override
    public boolean buy(Pet pet) {
        //更新主人表的元宝
        PetOwner petOwner = petOwnerDao.getOwnerByID(pet.getOwner_id());
        petOwner.setMoney(petOwner.getMoney() - pet.getPrice());
        //添加新账目
        Account account = new Account(1, pet.getId(), pet.getStore_id(), petOwner.getId(), pet.getPrice());
        pet.setStore_id(0);
        boolean result = false;//是否卖出成功
        BaseDao baseDao = new BaseDao();
        Connection conn = baseDao.getConn();
        PreparedStatement pstm = null;
        try {
            //关闭自动提交
            conn.setAutoCommit(false);
            result = petDao.updatePet(pet, conn, pstm)
                    && petOwnerDao.updatePetOwner(petOwner, conn, pstm)
                    && accountDao.insertAccount(account, conn, pstm);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            baseDao.close(conn, pstm);
        }
        return result;
    }

    /**
     * 批量购买宠物
     * 传入一个宠物对象集合,更改宠物主人
     * 更改元宝数，添加新帐目
     *
     * @param pets 宠物对象
     */
    @Override
    public boolean buyBatch(List<Pet> pets) {
        boolean result = false;//是否买入成功
        BaseDao baseDao = new BaseDao();
        Connection conn = baseDao.getConn();
        PreparedStatement pstm = null;
        try {
            //关闭自动提交
            conn.setAutoCommit(false);
            for (Pet pet:pets) {
                //更新主人表的元宝
                PetOwner petOwner = petOwnerDao.getOwnerByID(pet.getOwner_id());
                petOwner.setMoney(petOwner.getMoney() - pet.getPrice());
                //添加新账目
                Account account = new Account(1, pet.getId(), pet.getStore_id(), petOwner.getId(), pet.getPrice());
                pet.setStore_id(0);
                petDao.updatePet(pet, conn, pstm);
                petOwnerDao.updatePetOwner(petOwner, conn, pstm);
                accountDao.insertAccount(account, conn, pstm);
            }
            conn.commit();
            result = true;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            baseDao.close(conn, pstm);
        }
        return result;
    }


    /**
     * 卖出宠物
     * 传入一个宠物对象,更改宠物的主人
     * 主人元宝增加，商店余额增加，添加新帐目
     *
     * @param pet 宠物对象
     */
    @Override
    public boolean sell(Pet pet) {
        boolean result = false;//是否卖出成功
        BaseDao baseDao = new BaseDao();
        Connection conn = baseDao.getConn();
        PreparedStatement pstm = null;
        //更新主人表的元宝
        PetOwner petOwner = petOwnerDao.getOwnerByID(pet.getOwner_id());
        petOwner.setMoney(petOwner.getMoney() + pet.getPrice());
        //添加新账目
        Account account = new Account(2, pet.getId(), petOwner.getId(), pet.getStore_id(), pet.getPrice());
        pet.setOwner_id(0);
        try {
            //关闭自动提交
            conn.setAutoCommit(false);
            result = petDao.updatePet(pet, conn, pstm)
                    && petOwnerDao.updatePetOwner(petOwner, conn, pstm)
                    && accountDao.insertAccount(account, conn, pstm);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            baseDao.close(conn, pstm);
        }
        return result;
    }

    /**
     * 批量卖出宠物
     * 传入一个宠物对象集合,更改宠物的主人
     * 主人元宝增加，商店余额增加，添加新帐目
     *
     * @param pets 宠物对象集合
     */
    @Override
    public boolean sellBatch(List<Pet> pets) {
        boolean result = false;//是否卖出成功
        BaseDao baseDao = new BaseDao();
        Connection conn = baseDao.getConn();
        PreparedStatement pstm = null;
        try {
            //关闭自动提交
            conn.setAutoCommit(false);
            for (Pet pet:pets) {
                //更新主人表的元宝
                PetOwner petOwner = petOwnerDao.getOwnerByID(pet.getOwner_id());
                petOwner.setMoney(petOwner.getMoney() + pet.getPrice());
                //添加新账目
                Account account = new Account(2, pet.getId(), petOwner.getId(), pet.getStore_id(), pet.getPrice());
                pet.setOwner_id(0);
                petDao.updatePet(pet, conn, pstm);
                petOwnerDao.updatePetOwner(petOwner, conn, pstm);
                accountDao.insertAccount(account, conn, pstm);
            }
            conn.commit();
            result = true;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            baseDao.close(conn, pstm);
        }
        return result;
    }


//    /**
//     * 输入宠物主人id获得所有属于主人的宠物
//     *
//     * @param id 主人id
//     * @return 属于主人的宠物的集合
//     */
//    @Override
//    public List<Pet> getAllBelongsToPetOwner(int id) {
//        List<Pet> list = list = new ArrayList<>();
//        ;
//        List<Pet> allPet = petDao.getAllPet();
//        for (Pet pet : allPet) {
//            if (id == pet.getOwner_id()) {
//                list.add(pet);
//            }
//        }
//        return list;
//    }

    /**
     * 根据宠物主人id获得宠物主人对象
     *
     * @param id 宠物主人id
     * @return 宠物主人对象
     */
    @Override
    public PetOwner getOwnerByID(int id) {
        return petOwnerDao.getOwnerByID(id);
    }
}
